package com.topvision.myviewtest.gscview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.List;

/**
 * @author lucy
 * @date 2018-10-18 11:40
 * @description //TODO
 */

public class PieView extends View {
    /**
     * 画笔
     */
    private Paint mPaint;

    /**
     * 颜色
     */
    private int[] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080,
            0xFFE6B800, 0xFF7CFC00};

    /**
     * 起始角度
     */
    private float mStartAngle;

    /**
     * 宽高
     */
    private int mWidth, mHeight;

    /**
     * 数据
     */
    private List<PieData> mData;

    /**
     * 矩形
     */
    RectF mRectF = new RectF();

    public PieView(Context context) {
        this(context, null);
    }

    public PieView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PieView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mData == null) return;
        // 指向中心点
        canvas.translate(mWidth / 2, mHeight / 2);
        // 半径
        float r = (float) (Math.min(mWidth / 2, mHeight / 2) * 0.8);
        // 矩形
        mRectF.set(-r, -r, r, r);
        // 当前起始角度
        float currentStartAngle = mStartAngle;
        for (PieData data : mData) {
            float angle = data.getAngle();
            int color = data.getColor();
            mPaint.setColor(color);
            canvas.drawArc(mRectF, currentStartAngle, angle, true, mPaint);
            currentStartAngle += angle;
        }
    }

    public void setStartAngle(float angle) {
        mStartAngle = angle;
        invalidate();
    }

    /**
     * 外部调用
     */
    public void setData(List<PieData> data) {
        mData = data;
        initData(mData);
        invalidate();
    }

    private void initData(List<PieData> data) {
        if (data == null || data.size() <= 0) {
            return;
        }
        float sumValue = 0;
        for (int i = 0; i < data.size(); i++) {
            PieData pieData = data.get(i);
            float value = pieData.getValue();
            // 计算总数值
            sumValue += value;
            //循环赋值颜色
            pieData.setColor(mColors[i % mColors.length]);
        }

        for (PieData pieData : data) {
            float value = pieData.getValue();
            // 计算百分比
            float percentage = value / sumValue;
            // 设置百分比
            pieData.setPercentage(percentage);
            // 计算角度
            float angle = percentage * 360;
            // 设置角度
            pieData.setAngle(angle);
        }
    }
}
